package com.example.botdemo02.strategy.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.example.botdemo02.strategy.properties.OssConfigProperties;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.InputStream;

/**
 * oss上传策略
 *
 */
@Service("ossUploadStrategyImpl")
public class OssUploadStrategyImpl extends AbstractUploadStrategyImpl {

    @Resource
    private OssConfigProperties ossConfigProperties;


    @Override
    public void upload(String path, String fileName, InputStream inputStream) {
        OSS ossClient = getOssClient();
        ossClient.putObject(ossConfigProperties.getBucketName(), path + fileName, inputStream);
        ossClient.shutdown();
    }

    @Override
    public String getFileAccessUrl(String filePath) {
        return ossConfigProperties.getUrl() + filePath;
    }

    /**
     * 获取ossClient
     *
     * @return {@link OSS} ossClient
     */
    private OSS getOssClient() {
        return new OSSClientBuilder().build(ossConfigProperties.getEndpoint(), ossConfigProperties.getAccessKeyId(), ossConfigProperties.getAccessKeySecret());
    }
}
